package com.gdziarmaga.webshop.client.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gdziarmaga.webshop.client.model.BookClient;
import com.gdziarmaga.webshop.client.model.BookClientDAO;
import com.gdziarmaga.webshop.client.model.Client;
import com.gdziarmaga.webshop.client.model.ClientDAO;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;
import com.gdziarmaga.webshop.utils.filtering.FilterComparisonType;
import com.gdziarmaga.webshop.utils.filtering.FilterField;

@Service("clientManager")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class ClientManagerImpl implements ClientManager {

	@Autowired
	private ClientDAO clientDAO;

	@Autowired
	private BookClientDAO bookClientDAO;

	@Override
	public Client getClientById(Long clientId) {
		return clientDAO.getById(clientId);
	}

	@Override
	public List<Client> getClientByFilter(BaseFilter filter) {
		return clientDAO.getAll(filter);
	}

	@Override
	public Client getClientByFirst_name(String first_name) {
		return clientDAO.findProductByFirst_name(first_name);
	}

	@Override
	public Client getClientByLast_name(String last_name) {
		return clientDAO.findProductByLast_name(last_name);
	}

	@Override
	public void saveClient(Client client) {
		clientDAO.saveOrUpdate(client);

	}

	@Override
	public Client getClientByEmail(String email) {
		return clientDAO.findProductByEmail(email);
	}

	@Override
	public List<BookClient> getBooksOfClient(Client client) {
		BaseFilter base = new BaseFilter();
		base.addFilterField(new FilterField<Client>(client, "client",
				FilterComparisonType.EQUAL));

		return bookClientDAO.getAll(base);
	}

	@Override
	public void saveClientBook(BookClient bookClient) {
		bookClientDAO.saveOrUpdate(bookClient);
	}

	@Override
	public void removeClientById(Long clientId) {
		clientDAO.delete(clientId);
	}

}
